$(document).ready(function(){
	var cv = $("#cv");
	var ctx = cv.get(0).getContext("2d");
	var cv_w = cv.width();
	var cv_h = cv.height();

	$(window).resize(cv_resize);
	function cv_resize(){
		cv.attr("width",$(window).get(0).innerWidth);
		cv.attr("height",$(window).get(0).innerHeight);
		cv_w =cv.width();
		cv_h = cv.height();
	}
	cv_resize();

	var play_animation = true;
	var btn_start = $("#btn_start");
	var btn_stop = $("#btn_stop");

	btn_start.hide();
	btn_start.click(function(){
		$(this).hide();
		btn_stop.show();
		play_animation = true;
		animate();
	});

	btn_stop.click(function(){
		$(this).hide();
		btn_start.show();
		play_animation = false;
	});

	var Asteroid = function(x,y,radius,mass,vx,vy,ax,ay){
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.mass = mass;

		this.vx = vx;
		this.vy = vy;
		this.ax = ax;
		this.ay = ay;
	};

	var asteroids = new Array();
	for(var i = 0;i < 100;i++){
		var x = 20+(Math.random()*(cv_w-40));
		var y = 20+(Math.random()*(cv_h-40));
		var radius = 5+Math.random()*10;
		var mass = radius / 2;

		var vx = Math.random()*4-2;
		var vy = Math.random()*4-2;
		var ax = Math.random()*0.2 - 0.1;
		var ay = Math.random()*0.2 - 0.1;
		//var ax = 0;
		//var ay = 0;

		asteroids.push(new Asteroid(x,y,radius,mass,vx,vy,ax,ay));
	}

	function animate(){
		ctx.clearRect(0,0,cv_w,cv_h);
		ctx.fillStyle = "rgb(255,255,255)";
		for(var i=0;i<asteroids.length;i++){
			var asteroid = asteroids[i];

			for(var j = i+1;j < asteroids.length;j++){
				var other = asteroids[j];
				var dx = other.x - asteroid.x;
				var dy = other.y - asteroid.y;
				var distance = Math.sqrt(dx*dx+dy*dy);
				if(distance < asteroid.radius + other.radius){
					var angle = Math.atan2(dy,dx);
					var sine = Math.sin(angle);
					var cosine = Math.cos(angle);

					var x = 0;
					var y = 0;
					var xb = dx * cosine + dy * sine;
					var yb = dy * cosine - dx * sine;
					var vx = asteroid.vx * cosine + asteroid.vy * sine;
					var vy = asteroid.vy * cosine - asteroid.vx * sine;
					var vx2 = other.vx * cosine + other.vy * sine;
					var vy2 = other.vy * cosine - other.vx * sine;

					//vx *= -1;
					//vx2 *= -1;
					var vt = vx - vx2;
					vx = ((asteroid.mass - other.mass) * vx + 2 * other.mass * vx2)/(asteroid.mass + other.mass);
					vx2 = vt + vx;

					xb = x + (asteroid.radius + other.radius);

					asteroid.x = asteroid.x + (x * cosine - y * sine);
					asteroid.y = asteroid.y + (y * cosine + x * sine);
					other.x = asteroid.x + (xb * cosine - yb * sine);
					other.y = asteroid.y + (yb * cosine + xb * sine);

					asteroid.vx = vx * cosine - vy * sine;
					asteroid.vy = vy * cosine - vx * sine;
					other.vx = vx2 * cosine - vy2 * sine;
					other.vy = vy2 * cosine + vx2 * sine;
				}
			}

			asteroid.x += asteroid.vx;
			asteroid.y += asteroid.vy;
			if(Math.abs(asteroid.vx) < 10){
				asteroid.vx += asteroid.ax;
			}
			
			if(Math.abs(asteroid.vy) < 10){
				asteroid.vy += asteroid.ay;
			}

			/*
			if(Math.abs(asteroid.vx) > 0.1){
				asteroid.vx *= 0.9;
			}else{
				asteroid.vx = 0;
			}

			if(Math.abs(asteroid.vy) > 0.1){
				asteroid.vy *= 0.9;
			}else{
				asteroid.vy = 0;
			}
			*/

			if(asteroid.x - asteroid.radius < 0){
				asteroid.x = asteroid.radius;
				asteroid.vx *= -1;
				asteroid.ax *= -1;
			}else if(asteroid.x + asteroid.radius > cv_w){
				asteroid.x = cv_w - asteroid.radius;
				asteroid.vx *= -1;
				asteroid.ax *= -1;
			}

			if(asteroid.y - asteroid.radius < 0){
				asteroid.y = asteroid.radius;
				asteroid.vy *= -1;
				asteroid.ay *= -1;
			}else if(asteroid.y + asteroid.radius > cv_h){
				asteroid.y = cv_h - asteroid.radius;
				asteroid.vy *= -1;
				asteroid.ay *= -1;
			}

			ctx.beginPath();
			ctx.arc(asteroid.x,asteroid.y,asteroid.radius,0,Math.PI*2,false);
			ctx.closePath();
			ctx.fill();
		}
		if(play_animation){
			setTimeout(animate,33);
		}
	}
	animate();
});